package com.kuns.subject.application.controller;


import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Preconditions;
import com.kuns.subject.application.convert.SubjectAnswerDTOConverter;
import com.kuns.subject.application.convert.SubjectInfoDTOConverter;
import com.kuns.subject.application.dto.SubjectAnswerDTO;
import com.kuns.subject.application.dto.SubjectInfoDTO;
import com.kuns.subject.common.entity.PageResult;
import com.kuns.subject.common.entity.Result;
import com.kuns.subject.domian.entity.SubjectAnswerBO;
import com.kuns.subject.domian.entity.SubjectInfoBO;
import com.kuns.subject.domian.service.SubjectInfoDomainService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/subject")
@Slf4j
public class SubjectController {


    @Resource
    private SubjectInfoDomainService subjectInfoDomainService;

    /**
     * 新增题目
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectController.add.subjectInfoDTO:{}", JSONObject.toJSONString(subjectInfoDTO));
            }
            // 断言校验
            Preconditions.checkArgument(!StringUtils.isBlank(subjectInfoDTO.getSubjectName()), "题目名称不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectDifficult(), "题目的难度不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectType(), "题目的类型不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getSubjectScore(), "题目的分数不能为空");
            Preconditions.checkArgument(!CollectionUtils.isEmpty(subjectInfoDTO.getCategoryIds())
                    , "分类id不能为空");
            Preconditions.checkArgument(!CollectionUtils.isEmpty(subjectInfoDTO.getLabelIds())
                    , "标签id不能为空");
            // 将dto转成bo给domain层
            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE.convertDtoToBo(subjectInfoDTO);
            List<SubjectAnswerBO> subjectAnswerBOList = SubjectAnswerDTOConverter.INSTANCE.convertDtoListToBoList(subjectInfoDTO.getOptionList());
            subjectInfoBO.setOptionList(subjectAnswerBOList);
            subjectInfoDomainService.add(subjectInfoBO);
            return Result.ok(true);
        } catch (Exception e) {
            log.error("SubjectController.add.error:{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }


    /**
     * 查询题目列表
     */
    @PostMapping("getSubjectPage")
    public Result<PageResult<SubjectInfoDTO>> getSubjectPage(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectController.getSubjectPage.subjectInfoDTO:{}", JSONObject.toJSONString(subjectInfoDTO));
            }
            Preconditions.checkNotNull(subjectInfoDTO.getCategoryId(), "分类id不能为空");
            Preconditions.checkNotNull(subjectInfoDTO.getLabelId(), "标签id不能为空");
            // 将dto转成bo
            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE.convertDtoToBo(subjectInfoDTO);
            subjectInfoBO.setPageNo(subjectInfoDTO.getPageNo());
            subjectInfoBO.setPageSize(subjectInfoDTO.getPageSize());
            PageResult<SubjectInfoBO> pageResult = subjectInfoDomainService.queryPage(subjectInfoBO);
            return Result.ok(pageResult);
        } catch (Exception e) {
            log.error("SubjectController.getSubjectPage.error:{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 查询题目信息
     */
    @PostMapping("getSubjectInfo")
    public Result<SubjectInfoDTO> getSubjectInfo(@RequestBody SubjectInfoDTO subjectInfoDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectController.getSubjectInfo.subjectInfoDTO:{}", JSONObject.toJSONString(subjectInfoDTO));
            }
            Preconditions.checkNotNull(subjectInfoDTO.getId(), "题目id不能为空");
            // 将dto转换为bo
            SubjectInfoBO subjectInfoBO = SubjectInfoDTOConverter.INSTANCE.convertDtoToBo(subjectInfoDTO);
            SubjectInfoBO boResult = subjectInfoDomainService.querySubjectInfo(subjectInfoBO);
            SubjectInfoDTO dtoResult = SubjectInfoDTOConverter.INSTANCE.convertBoToDto(boResult);
            List<SubjectAnswerDTO> subjectAnswerDTOList = SubjectAnswerDTOConverter.INSTANCE.convertBoListToDtoList(boResult.getOptionList());
            dtoResult.setOptionList(subjectAnswerDTOList);
            return Result.ok(dtoResult);
        } catch (Exception e) {
            log.error("SubjectController.getSubjectInfo.error:{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }

    }


}
